package com.archstoday.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.archstoday.R;
import com.archstoday.entity.ArticleListEntity;
import com.base.utils.ImageUtil;

import java.util.List;

/**
 * 用户文章列表页面适配器
 */
public class ArticleListAdapter extends BaseAdapter {

    private Context mContext;

    private List<ArticleListEntity.DataBean.ArticleListBean> mData;

    public ArticleListAdapter(Context context) {
        mContext = context;
    }

    public void setData(List<ArticleListEntity.DataBean.ArticleListBean> data) {
        mData = data;
        notifyDataSetChanged();
    }

    public void addData(List<ArticleListEntity.DataBean.ArticleListBean> data) {
        mData.addAll(data);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        if (mData != null) {
            return mData.size();
        }
        return 0;
    }

    @Override
    public ArticleListEntity.DataBean.ArticleListBean getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        Holder holder;
        if (view == null) {
            holder = new Holder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item_article_list, null);
            holder.icon = (ImageView) view.findViewById(R.id.item_icon);
            holder.titleTv = (TextView) view.findViewById(R.id.item_title_tv);
            holder.commentTv = (TextView) view.findViewById(R.id.item_comment_tv);
            holder.typeTv = (TextView) view.findViewById(R.id.item_type_tv);

            view.setTag(holder);
        } else {
            holder = (Holder) view.getTag();
        }

        ImageUtil.setCropBitmap(mContext,
                holder.icon, mData.get(i).getImgs(), R.mipmap.default_icon);
        holder.titleTv.setText(mData.get(i).getTitle());
        holder.commentTv.setText(String.valueOf(mData.get(i).getComments()));

        switch (mData.get(i).getStatus()) {
            case "0":
                holder.typeTv.setBackgroundResource(R.drawable.item_state_bg_gray);
                holder.typeTv.setTextColor(ImageUtil.getColor(mContext, R.color.item_text));
                holder.typeTv.setText("未知");
                break;
            case "1":
                holder.typeTv.setBackgroundResource(R.drawable.item_state_bg_yellow);
                holder.typeTv.setTextColor(ImageUtil.getColor(mContext, R.color.yellow_state_bg));
                holder.typeTv.setText("待审核");
                break;
            case "2":
                holder.typeTv.setBackgroundResource(R.drawable.item_state_bg_green);
                holder.typeTv.setTextColor(ImageUtil.getColor(mContext, R.color.green_text));
                holder.typeTv.setText("已上线");
                break;
            case "3":
                holder.typeTv.setBackgroundResource(R.drawable.item_state_bg_yellow);
                holder.typeTv.setTextColor(ImageUtil.getColor(mContext, R.color.yellow_state_bg));
                holder.typeTv.setText("待审核");
                break;
            case "4":
                holder.typeTv.setBackgroundResource(R.drawable.item_state_bg_gray);
                holder.typeTv.setTextColor(ImageUtil.getColor(mContext, R.color.item_text));
                holder.typeTv.setText("已下线");
                break;
            case "5":
                holder.typeTv.setBackgroundResource(R.drawable.item_state_bg_gray);
                holder.typeTv.setTextColor(ImageUtil.getColor(mContext, R.color.item_text));
                holder.typeTv.setText("已删除");
                break;
        }

        return view;
    }

    class Holder {
        ImageView icon;
        TextView titleTv, commentTv, typeTv;
    }
}